//
//  ESToolbar.m
//  测试视频
//
//  Created by lanou3g on 15/6/30.
//  Copyright (c) 2015年 不成熟. All rights reserved.
//

#import "ESToolbar.h"
#define kFontSize 14

@interface ESToolbar ()
@property (nonatomic,weak)UIImageView *boundaryLine;
@end

@implementation ESToolbar

+ (instancetype)toolbar
{
    return [[self alloc] init];
}

+ (instancetype)cellButtomView {
    return [[ESToolbar alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {

        UIImageView *boundaryLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-content-line"]];
        self.boundaryLine = boundaryLine;
        [self addSubview:_boundaryLine];
        [self setupBtn:@"顶" image:@"mainCellDingN" highImage:@"mainCellDingClickN" ESToolbarBtnType: ESToolbarBtnTypeDing];
        [self setupBtn:@"踩" image:@"mainCellCaiN" highImage:@"mainCellCaiClickN" ESToolbarBtnType: ESToolbarBtnTypeCai];
        [self setupBtn:@"分享" image:@"mainCellShareN" highImage:@"mainCellShareClickN" ESToolbarBtnType: ESToolbarBtnTypeShare];
        [self setupBtn:@"评论" image:@"mainCellCommentN" highImage:@"mainCellCommentClickN" ESToolbarBtnType: ESToolbarBtnTypeComment];
 
    }
    return self;
}

//初始化一个按钮
- (void)setupBtn:(NSString *)title image:(NSString *)image highImage:(NSString *)highImage ESToolbarBtnType:(NSUInteger)type
{
    UIButton *btn = [[UIButton alloc] init];
    btn.tag = type;
    [btn setImage:[UIImage imageNamed:image] forState:(UIControlStateNormal)];
    [btn setImage:[UIImage imageNamed:highImage] forState:(UIControlStateHighlighted)];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    btn.titleLabel.font = [UIFont systemFontOfSize:kFontSize];
    [btn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [btn setTitle:title forState:(UIControlStateNormal)];
 
    [self addSubview:btn];
}

- (void)btnClick:(UIButton *)btn
{
    if (_delegate != nil && [_delegate respondsToSelector:@selector(ESToolbar:didSelectBtn:)]) {
        [_delegate ESToolbar:self didSelectBtn:btn];
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.boundaryLine.x = 0;
    self.boundaryLine.y = 0;
    self.boundaryLine.width = self.width;
    self.boundaryLine.height = 1;
    self.boundaryLine.alpha = 0.3;
    NSUInteger count = 4;
    CGFloat btnW = self.width/count;
    CGFloat btnH = self.height;
    for (NSUInteger i = 0 ; i < count; i++) {
        UIButton *btn = (UIButton *)[self viewWithTag:(i + ESToolbarBtnTypeDing)];
        btn.y = 0;
        btn.x = i * btnW;
        btn.height = btnH;
        btn.width = btnW;
    }
    
    for (NSUInteger i = 0; i < 3; i++) {
        CGFloat devideLineX = (i + 1)*(self.width/4);
        CGFloat devideLineY = 5;
        CGFloat devideLineH = self.height - 10;
        CGFloat devideLineW = 1;
        UIImageView *devideView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-button-line"]];
        devideView.frame = CGRectMake(devideLineX, devideLineY, devideLineW, devideLineH);
        [self addSubview:devideView];
    }
}

@end
